package com.theshy.test.domain;

import com.theshy.test.dao.Student;

public class StudentDao implements BassStudentDao{

    static Student[] students = new Student[3];

    static {
        Student s1 = new Student(204911,"wbk",20,"2000");
        Student s2 = new Student(204912,"zmh",20,"2000");
        students[0]=s1;
        students[1]=s2;
    }

    public boolean Addstudent(Student s) {

        int index = -1;

        for (int i = 0; i < students.length; i++) {
            Student stu = students[i];
            if (stu == null) {
                index = i;
                break;
            }
        }

        if (index == -1) {
            return false;
        } else {
            students[index] = s;
            return true;
        }

    }

    public boolean haveID(int id) {

        for (int i=0;i<students.length;i++) {
            if(students[i]!=null&&students[i].getId()==id){
                return true;
            }
        }

        return false;
    }

    public Student[] checkStudent() {

        return students;
    }

    public void DeleteStudent(int id) {

           int index = getIndex(id);

           students[index]=null;
    }

    public int getIndex(int id){
         int  index = -1;

         for(int i=0;i<students.length;i++){
             if(students[i]!=null&&students[i].getId()==id){
                 index = i;
                 break;
             }
         }
        return index;
    }

    public boolean ChangeStudent(Student student) {

        int index = getIndex(student.getId());
        if(index!=-1){
            students[index]=student;
            return true;
        }else{
            return false;
        }

    }
}
